/*
 * MIT License
 */

package lib.util;

import java.util.TreeSet;

/**
 *
 * @author dolphinigle
 */
public class StringUtil {
  /**
   * Transforms input into the corresponding String[]
   */
  public static String[] charArrayToStringArray(char[][] param) {
    String[] ret = new String[param.length];
    for (int i = 0; i < ret.length; ++i) {
      ret[i] = String.valueOf(param[i]);
    }
    return ret;
  }
  
  /**
   * Transforms input into the corresponding char[][]
   */
  public static char[][] StringArrayToCharArray(String[] param) {
    assert param.length > 0;
    char[][] ret = new char[param.length][param[0].length()];
    for (int i = 0; i < param.length; ++i) {
      for (int j = 0; j < param[0].length(); ++j) {
        ret[i][j] = param[i].charAt(j);
      }
    }
    return ret;
  }
  
  /**
   * Transposes a matrix
   */
  public static char[][] Transpose(char[][] param) {
    char[][] result = new char[param[0].length][param.length];
    
    for (int i = 0; i < param.length; ++i)
      for (int j = 0; j < param[0].length; ++j)
        result[j][i] = param[i][j];
    
    return result;
  }

  /**
   * Fill content of param with fill.
   * @param param
   * @param fill
   */
  public static void fillCharArrayArray(char[][] param, char fill) {
    for (int i = 0; i < param.length; ++i) {
      for (int j = 0; j < param[i].length; ++j) {
        param[i][j] = fill;
      }
    }
  }
  
  /**
   * Create a string of identical characters.
   * @param length length of string to be created
   * @param init_char character fill of the string
   */
  public static String create(int length, char init_char) {
    String ret = "";
    for (int i = 0; i < length; ++i) {
      ret += init_char;
    }
    return ret;
  }

  /**
   * Creates a string by copying input
   */
  public static String create(int length, String base) {
    if (length == 0) return "";
    assert length > 0;
    assert base.length() > 0;
    String ret = "";
    for (int i = 0; i < length; ++i) {
      ret += base.charAt(i % base.length());
    }
    return ret;
  }
  
  /**
   * Removes all four borders if they consist only of the specified characters.
   */
  public static char[][] RemoveBorders(char[][] array, String str) {
    TreeSet<Character> ch = new TreeSet<Character>();
    for (int i = 0; i < str.length(); ++i) {
      ch.add(str.charAt(i));
    }
    int uprow = 0;
    int downrow = array.length - 1;
    int upcol = 0;
    int downcol = array[0].length - 1;
    
    for (int i = 0; i < array.length; ++i) {
      boolean ok = true;
      for (int j = 0; j < array[0].length; ++j) {
        if (!ch.contains(array[i][j])) {
          ok = false;
        }
      }
      if (!ok) {
        uprow = i;
        break;
      }
    }

    for (int i = 0; i < array.length; ++i) {
      boolean ok = true;
      for (int j = 0; j < array[0].length; ++j) {
        if (!ch.contains(array[i][j])) {
          ok = false;
        }
      }
      if (!ok) {
        downrow = i;
      }
    }

    for (int i = 0; i < array[0].length; ++i) {
      boolean ok = true;
      for (int j = 0; j < array.length; ++j) {
        if (!ch.contains(array[j][i])) {
          ok = false;
        }
      }
      if (!ok) {
        upcol = i;
        break;
      }
    }

    for (int i = 0; i < array[0].length; ++i) {
      boolean ok = true;
      for (int j = 0; j < array.length; ++j) {
        if (!ch.contains(array[j][i])) {
          ok = false;
        }
      }
      if (!ok) {
        downcol = i;
      }
    }
    
    assert(uprow <= downrow);
    assert(upcol <= downcol);
    
    char[][] ret = new char[downrow - uprow + 1][downcol - upcol + 1];
    
    for (int i = uprow; i <= downrow; ++i) {
      for (int j = upcol; j <= downcol; ++j) {
        ret[i - uprow][j - upcol] = array[i][j];
      }
    }
    
    return ret;
  }
}
